package com.mall507.fivepeaks.activity.community;

import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.View;

import com.alibaba.android.arouter.launcher.ARouter;
import com.google.gson.Gson;
import com.hzh.frame.ui.fragment.AbsRecyclerViewFM;
import com.hzh.frame.widget.rxbus.MsgEvent;
import com.hzh.frame.widget.rxbus.RxBus;
import com.hzh.frame.widget.xrecyclerview.RecyclerViewHolder;
import com.mall507.comn.model.Art;
import com.mall507.comn.model.Original;
import com.mall507.fivepeaks.R;
import com.mall507.fivepeaks.widget.imageview.CircleImageView;
import com.mall507.fivepeaks.widget.imageview.ExpandImageView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Predicate;

/**
 * 文化艺术
 */
public class CommunityArtRFM extends AbsRecyclerViewFM<Art> {
    public static final String TAG="CommunityArtRFM";

    public static CommunityArtRFM newInstance(int type) {
        CommunityArtRFM myFragment = new CommunityArtRFM();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        myFragment.setArguments(bundle);
        return myFragment;
    }

    @Override
    public boolean setTitleIsShow() {
        return false;
    }

    @Override
    protected int setLayoutId() {
        return R.layout.fm_rv_community_art;
    }

    @Override
    protected void bindView(View view) {
        super.bindView(view);
        showLoding();
        initBus();
    }

    @Override
    protected String setHttpPath() {
        return "Community/getArtBlog";
    }

    @Override
    protected JSONObject setHttpParams() {
        JSONObject params = new JSONObject();
        try {
            params.put("type", getArguments().getInt("type"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }

    @Override
    protected List<Art> handleHttpData(JSONObject response) {
        List<Art> list = new ArrayList<>();
        if (200 == response.optInt("code")) {
            JSONArray originals = response.optJSONArray("data");
            if (originals != null && originals.length() > 0) {
                for (int i = 0; i < originals.length(); i++) {
                    JSONObject object = originals.optJSONObject(i);
                    JSONObject member = object.optJSONObject("member");
                    Art model = new Art();
                    model.setNid(object.optString("id"));
                    model.setImage(object.optString("cover_img"));
                    model.setTitle(object.optString("title"));
                    if (member != null && member.length()>0){
                        model.setHeadImage(member.optString("head_img"));
                        model.setNice(member.optString("nickname"));
                    }
                    model.setTime(object.optString("create_at"));
                    model.setContent(object.optString("content"));
                    model.setBrowseNumber(object.optString("views"));
                    model.setLickNumber(object.optString("collection_count"));
                    list.add(model);
                }
            }
        }
        dismissLoding();
        return list;
    }

    @Override
    protected void handleHttpDataFailure() {
        super.handleHttpDataFailure();
        showLodingFail();
    }



    @Override
    protected int setItemLayoutId(int viewType) {
        return R.layout.item_rv_community_art;
    }

    @Override
    protected void bindItemData(RecyclerViewHolder holder, final int position, final Art model) {
        ((ExpandImageView) holder.getView(R.id.image)).setImageURI(model.getImage());
        holder.setText(R.id.title, model.getTitle());
        ((CircleImageView) holder.getView(R.id.head)).setImageURI(model.getHeadImage());
        holder.setText(R.id.nice, model.getNice());
        holder.setText(R.id.time, model.getTime());
//        holder.setText(R.id.content, model.getContent());

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            holder.setText(R.id.content, String.valueOf(Html.fromHtml(model.getContent(),Html.FROM_HTML_MODE_COMPACT)));
        }else {
            holder.setText(R.id.content, String.valueOf(Html.fromHtml(model.getContent())));
        }
        holder.setText(R.id.browseNumber, model.getBrowseNumber() + "人浏览");
        holder.setText(R.id.lickNumber, model.getLickNumber());
        holder.setOnItemClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ARouter.getInstance()
                        .build("/comn/ComnCultureItemInfoRUI")
                        .withString("id", model.getNid())
                        .withString("commentRequestPath","Comment/getArtComment")
                        .withString("headRequestPath","Community/getArtBlog")
                        .withString("type","4")
                        .withString("position", position+"")
                        .withString("collection_count",model.getLickNumber())
                        .navigation();
            }
        });
    }

    public void initBus() {
        RxBus.getInstance()
                .toObservable(getViewLifecycleOwner(), MsgEvent.class)
                .filter(new Predicate<MsgEvent>() {
                    @Override
                    public boolean test(MsgEvent msgEvent) throws Exception {
                        return msgEvent.getTag().equals(TAG);
                    }
                })
                .subscribe(new Consumer<MsgEvent>() {
                    @Override
                    public void accept(MsgEvent msgEvent) throws Exception {
                        String msg= (String) msgEvent.getMsg();
                        HashMap<String,Object> map=new Gson().fromJson(msg, HashMap.class);
                        int position=Integer.parseInt(map.get("position").toString());
                        Art model=getAdapter().getDatas().get(position);
                        model.setLickNumber(map.get("collection").toString());
                        model.setBrowseNumber(map.get("views").toString());
                        getAdapter().notifyItemChanged(position);
                    }
                });
    }
    
}
